﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightMultiLevelNavigationExample
{
    public static class DependencyObjectExtensions
    {
        public static T FindVisualChild<T>(this DependencyObject instance) where T : DependencyObject
        {
            T control = default(T);

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(instance); i++)
            {
                if ((control = VisualTreeHelper.GetChild(instance, i) as T) != null)
                {
                    break;
                }

                control = FindVisualChild<T>(VisualTreeHelper.GetChild(instance, i));
            }

            return control;

        }
    }
}
